﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SmartMotion.Core.Tools;
using Sunny.UI;

namespace SmartMotion.Core
{
    public partial class FrmActionItem : UIPage
    {
        private readonly ActionItem _actionItem;
        private BindingList<MethodParms> _bindingList;
        private ConcurrentDictionary<string, VarInfo> _vars;
        public FrmActionItem(ActionItem actionItem)
        {
            _actionItem = actionItem;
            InitializeComponent();
            this.Load += FrmActionItem_Load;
        }

        private void FrmActionItem_Load(object sender, EventArgs e)
        {
            #region 初始化表格

            SetGrid();
            _vars = GlobalVars.Ins.Vars[GlobalVars.FlowVars];
            RefreshGridData();
            foreach (DataGridViewColumn dataGridViewColumn in grid.Columns)
            {
                dataGridViewColumn.ReadOnly = true;
            }
            grid.Columns[2].ReadOnly = false;

            #endregion

            #region 初始化底部控件
            cmbObj.Enabled = !_actionItem.IsStatic;
            cmbReturn.Enabled = !_actionItem.ReturnType.Contains("Void");
            lblStatic.Visible = _actionItem.IsStatic;
            cmbObj.Visible= !_actionItem.IsStatic;
            lblObj.Visible = !_actionItem.IsStatic;
            bool showRet= !_actionItem.ReturnType.Contains("Void");
            lblReturn.Visible = showRet;
            lblType.Visible = showRet;
            cmbReturn.Visible = showRet;
            GlobalVars.Ins.ComboTreeLoadData(cmbObj,typeof(object).FullName);
            cmbObj.Text = _actionItem.ObjName;
            GlobalVars.Ins.ComboTreeLoadData(cmbReturn,_actionItem.ReturnType);
            cmbReturn.Text = _actionItem.ReturnObjName;
            lblType.Text = _actionItem.ReturnType;
            #endregion
        }

        private void RefreshGridData()
        {
            _bindingList = new BindingList<MethodParms>(_actionItem.Parms);
            grid.DataSource = _bindingList;
        }

        private void SetGrid()
        {
            grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            grid.RowHeadersVisible = false;
            grid.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grid.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grid.AllowUserToResizeRows = false;
            grid.MultiSelect = false;
            grid.AllowUserToAddRows = false;
            grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void grid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            if (index < 0) return;
            if (!_bindingList[index].InputOrSelect)
            {
                var dialog = new FrmSelectVar(_bindingList[index]);
                dialog.Show();

            }
            else
            {
                var valueType = _bindingList[index].Type;
                //手动输入的临时变量
                if (valueType == typeof(int).FullName)
                {
                    int result = 0;
                    if (UIInputDialog.InputIntegerDialog(ref  result))
                    {
                        _bindingList[index].Value = result;
                    }
                }
                if (valueType == typeof(uint).FullName)
                {
                    int result = 0;
                    if (UIInputDialog.InputIntegerDialog(ref result))
                    {
                        _bindingList[index].Value = (uint)result;
                    }
                }
                if (valueType == typeof(short).FullName)
                {
                    int result = 0;
                    if (UIInputDialog.InputIntegerDialog(ref result))
                    {
                        _bindingList[index].Value = (short)result;
                    }
                }
                if (valueType == typeof(ushort).FullName)
                {
                    int result = 0;
                    if (UIInputDialog.InputIntegerDialog(ref result))
                    {
                        _bindingList[index].Value = (ushort)result;
                    }
                }
                if (valueType == typeof(long).FullName)
                {
                    int result = 0;
                    if (UIInputDialog.InputIntegerDialog(ref result))
                    {
                        _bindingList[index].Value = (long)result;
                    }
                }
                if (valueType == typeof(ulong).FullName)
                {
                    int result = 0;
                    if (UIInputDialog.InputIntegerDialog(ref result))
                    {
                        _bindingList[index].Value = (long)result;
                    }
                }

                if (valueType == typeof(Single).FullName)
                {
                    double result = 0;
                    if (UIInputDialog.InputDoubleDialog(ref result))
                    {
                        _bindingList[index].Value = (Single)result;
                    }
                }
                if (valueType == typeof(double).FullName)
                {
                    double result = 0;
                    if (UIInputDialog.InputDoubleDialog(ref result))   
                    {
                        _bindingList[index].Value = (double)result;
                    }
                }

                if (valueType == typeof(string).FullName)
                {
                    string result = string.Empty;
                    if (UIInputDialog.InputStringDialog(ref result))
                    {
                        _bindingList[index].Value = result;
                    }
                }
                if (valueType == typeof(bool).FullName)
                {
                    List<bool> listBool = new List<bool>() {true, false};
                    int result = 0;
                    if (this.ShowSelectDialog(ref result,listBool))
                    {
                        _bindingList[index].Value = listBool[result];
                    }
                }
            }
            RefreshGridData();

        }

        private void cmbObj_NodeSelected(object sender, TreeNode node)
        {
            _actionItem.ObjName = node.Text;
            _actionItem.ObjGuid = node.Tag.ToString();
        }

        private void cmbReturn_NodeSelected(object sender, TreeNode node)
        {
            _actionItem.ReturnObjName = node.Text;
            _actionItem.ReturnObjGuid = node.Tag.ToString();

        }
    }   
}
